package com.GlobalPaint.app.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.PaletteDetailsBean;
import com.GlobalPaint.app.ui.HTML.HomeDetailsHTML;
import com.GlobalPaint.app.utils.Constants;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2017/12/22 0022.
 */

public class HomeAdapter extends XRecyclerView.Adapter<HomeAdapter.ViewHoler> {
    private List<PaletteDetailsBean.DataBean> list;
    private Context context;
    public HomeAdapter(List<PaletteDetailsBean.DataBean> list, Context context) {
        this.list=list;
        this.context=context;
    }

    @Override
    public HomeAdapter.ViewHoler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_palette_item, parent, false);
        ViewHoler holder = new ViewHoler(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(HomeAdapter.ViewHoler holder, final int position) {
        if(list.size()>0){
            String name=list.get(position).getFTitle()==
                    null?"":list.get(position).getFTitle();
            String sj=list.get(position).getFPublishDate()==
                    null?"":list.get(position).getFPublishDate();

            String Url= Constants.JASON_SERVICE_Img+list.get(position).getFSmallImage()==
                    null?"http://218.192.170.132/1.jpg":Constants.JASON_SERVICE_Img+list.get(position).getFSmallImage();

            String bt=list.get(position).getFMetaDescription()==
                    null?"":list.get(position).getFMetaDescription();
            Picasso.with(context).load(Url)
                    .into(holder.imge_palette);
            holder.tv_name.setText(name);
           /* if( holder.tv_sj.getText().length()>10){
                holder.tv_sj.setText(sj.substring(0,10));
            }*/

            holder.bt.setText(bt);


        }
        holder.mview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              /*  DataManager.typename = "文章1";

               List <PaletteDetailsBean.DataBean> Bean =  list;
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("serinfo", (Serializable) Bean);
                intent.putExtras(bundle);
                intent.putExtra("id",position);
                intent.setClass(context, textActivity.class);
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);*/
                Intent intent=new Intent(context, HomeDetailsHTML.class);
                intent.putExtra("position",position);
                intent.putExtra("fCreatorId",list.get(position).getFCreatorId());
                intent.putExtra("fInfoId",list.get(position).getFInfoId());
                view.getContext().startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHoler extends RecyclerView.ViewHolder {
        private final ImageView imge_palette;
        private final TextView tv_name;
        private final TextView tv_sj;
        private final TextView bt;
        View mview;

        public ViewHoler(View itemView) {
            super(itemView);
            mview = itemView;
            imge_palette = (ImageView)itemView.findViewById(R.id.imge_palette);
            tv_name = (TextView)itemView.findViewById(R.id.tv_name);
            tv_sj = (TextView) itemView.findViewById(R.id.tv_sj);
            bt = (TextView) itemView.findViewById(R.id.bt);
        }
    }
}
